package top.oylan.manager.controller.mp;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import top.oylan.manager.entity.Banner;
import top.oylan.manager.entity.Category;
import top.oylan.manager.entity.mp.MpRecommended;
import top.oylan.manager.entity.mp.MpResult;
import top.oylan.manager.service.BannerService;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.service.ProductService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-08-11
 */
@RestController
@RequestMapping("/mp/home")
@CrossOrigin
public class MpHomeController {

    @Resource
    ProductService productService;

    @Resource
    BannerService bannerService;

    @Resource
    CategoryService categoryService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public MpResult index(){

        MpResult result = new MpResult();
        JSONObject data = new JSONObject();

        // 1.banner
        ArrayList<Banner> banners = bannerService.selectBannerByPosition(1);
        data.put("banners",banners);

        List<MpRecommended> recommended = productService.selectRecommendedForMp();
        data.put("recommends",recommended);
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    public MpResult categories(@RequestParam("type")int type){
        MpResult result = new MpResult();
        List<Category> list = categoryService.selectCategoryByType(type);
        result.setData(list);
        return result;
    }
}
